<template>
  <view class="edit-password">
    <navbar title="设置">
      <view slot="left"/>
    </navbar>
    <view class="body">
      <view class="head">
        <view class="title">修改密码</view>
        <view class="desc">请设置您的新密码</view>
      </view>
      <form @submit="submit">
        <view class="form">
          <view class="form-item">
            <view class="form-label">新密码</view>
            <view class="form-con">
              <input :password="!showPwd" v-model="form.password" placeholder="请输入新密码" placeholder-class="place"/>
              <view class="suffix">
                <text :class="['iconfont', 'fz-40', (!showPwd? 'icon-bukejian' : 'icon-kejian')]"
                      @click="showPwd = !showPwd"></text>
              </view>
            </view>
          </view>
          <view class="form-item">
            <view class="form-label">再次输入</view>
            <view class="form-con code-item">
              <input :password="!showPwdTwo" v-model="form.twoPassword" placeholder="请再次输入密码"
                     placeholder-class="place"/>
              <view class="suffix">
                <text :class="['iconfont', 'fz-40', (!showPwdTwo? 'icon-bukejian' : 'icon-kejian')]"
                      @click="showPwdTwo = !showPwdTwo"></text>
              </view>
            </view>
          </view>
        </view>
        <button form-type="submit" class="btn-submit" @click="submit">确认修改</button>
      </form>
    </view>
    <c-modal v-model="show" @confirm="doSubmit">确认修改密码？</c-modal>
  </view>
</template>

<script>
import {changePassword} from "@/api/my";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      showPwd: false,
      showPwdTwo: false,
      show: false,
      form: {
        password: "",
        twoPassword: "",
      },
      rules: {
        password: [
          {required: true, message: "请输入新密码"},
        ],
        twoPassword: [
          {required: true, message: "请再次输入密码"}
        ]
      }
    };
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    submit() {
      this.$verify.verifyForm(this.form, this.rules).then(() => {
        if (this.form.password !== this.form.twoPassword) {
          this.$toast("两次输入的密码不一致");
          return;
        }
        this.show = true;
      })
    },
    doSubmit() {
      this.show = false;
      changePassword({
        miniOpenId: this.userInfo.miniOpenId,
        password: this.form.password,
      }).then(() => {
        this.$toast("密码修改成功", () => {
          uni.navigateBack();
        });
      })
    },
  }
}
</script>

<style lang="scss">
@import '@/static/styles/minix.scss';

page {
  background: #fff;
}

.edit-password {
  .body {
    padding: 120rpx 30rpx 0;
  }

  .head {
    .title {
      font-size: 48rpx;
    }

    .desc {
      font-size: 24rpx;
      color: $-color-muted;
    }
  }

  .form {
    margin-top: 60rpx;

    .form-item {
      margin-bottom: 32rpx;
      flex-direction: column;
      height: auto;
      line-height: inherit;

      .form-item {
        font-size: 32rpx;
      }

      .form-con {
        border: 1rpx solid #e6e6e6;
        height: 80rpx;
        display: flex;
        align-items: center;
        border-radius: 16rpx;
        padding: 0 20rpx;
        margin-top: 20rpx;

        input {
          flex: 1;
          margin-right: 20rpx;
        }
      }
    }

  }

  .btn-submit {
    width: 100%;
    height: 90rpx;
    line-height: 90rpx;
    font-size: 32rpx;
    color: #fff;
    background: #000;
    border-radius: 16rpx;
    margin-top: 70rpx;

    &[disabled] {
      background: #ccc;
    }
  }
}
</style>
